<?php
	namespace app\admin\controller;

	use \think\Db;

	class Ticket extends Base{

		public function index(){

			try{

				if($this->method=='post'){

					$page=input('post.page/d');

					$pagesize=10;

					$count=Db::name('ticket')->count();

					$allpage=ceil($count/$pagesize);

					$page=$page>$allpage?$allpage:$page;

					$page=$page>=1?$page:1;

					$start=($page-1)*$pagesize;

					$row=Db::name('ticket')->order('t_id desc')->limit($start,$pagesize)->select();

					return $this->success(['data'=>$row,'page'=>$page,'pagesize'=>$pagesize,'allpage'=>$allpage,'count'=>$count]);

				}else{

					echo '拒绝访问';

				}

			}catch(\Exception $e){

				return $this->err($e);

			}

		}

		public function edit(){

			try{

				if($this->method=='post'){

					$id=(int)input('post.id');

					$row=Db::name('ticket')->where(['t_id'=>$id])->find();

					if($row){

						return $this->success($row);

					}

					return $this->fail('该车票不存在~');

				}else{

					echo '拒绝访问';
						
				}


			}catch(\Exception $e){

				return $this->err($e);

			}

		}

		public function edit_save(){

			try{

				if($this->method=='post'){

					$id=(int)input('post.t_id');

					$data['t_name']=input('post.t_name');

					if($data['t_name']==''){

						return $this->fail('请输入车站名称~');

					}

					$data['t_start_place']=input('post.t_start_place/a');

					if(!$data['t_start_place']){

						return $this->fail('请设置起始站~');

					}

					$data['t_end_place']=input('post.t_end_place/a');

					if(!$data['t_end_place']){

						return $this->fail('请设置终点站~');

					}

					$data['t_time']=input('post.t_time');

					if(!$data['t_time']){

						return $this->fail('请输入发车时间~');

					}

					$data['t_time']=floor($data['t_time']/1000);

					$time=time();

					if($data['t_time']<$time){

						return $this->fail('发车时间不能小于当前时间~');

					}

					$data['t_seat']=input('post.t_seat/a');

					if(empty($data['t_seat'])){
						return $this->fail('请添加坐席');
					}

					$data['t_ticket_price']=(int)input('post.t_ticket_price');

					if(!$data['t_ticket_price']){

						return $this->fail('请输入车票均价~');

					}

					$data['t_departCity']=$data['t_start_place']['text'][1]=='市辖区' || $data['t_start_place']['text'][1]=='县'?$data['t_start_place']['text'][0]:$data['t_start_place']['text'][1];

					$data['t_destination']=$data['t_end_place']['text'][1]=='市辖区' || $data['t_end_place']['text'][1]=='县'?$data['t_end_place']['text'][0]:$data['t_end_place']['text'][1];

					$row=Db::name('ticket')->where(['t_id'=>$id])->find();

					foreach($data['t_seat'] as $k=>$v){
						$data['t_seat'][$k]['count']=(int)$v['count'];
					}

					if($row){

						$res=Db::name('ticket')->where(['t_id'=>$id])->update($data);

					}else{

						$primary=Db::name('primary')->where(['type'=>'ticket'])->find();

						if($primary){

							$data['t_id']=$primary['id'];

							$res=Db::name('primary')->where(['type'=>'ticket'])->setInc('id',1);

						}else{

							$data['t_id']=1;

							$res=Db::name('primary')->insert(['type'=>'ticket','id'=>2]);

						}

						$res2=Db::name('ticket')->insert($data);

					}

					return $this->success();

				}else{

					echo '拒绝访问';

				}

			}catch(\Exception $e){

				return $this->err($e);

			}

		}

		public function dele(){

			try{

				if($this->method=='post'){

					$id=(int)input('post.id');

					$res=Db::name('ticket')->where(['t_id'=>$id])->delete();

					if($res){

						return $this->success();

					}

					return $this->fail('删除失败~');

				}else{

					echo '拒绝访问';
						
				}

			}catch(\Exception $e){

				return $this->err($e);

			}

		}

	}

?>